import { defineStore } from 'pinia'
import { getAllType } from "@/api/base.js";

export const useCreate = defineStore('create', {
    state: () => ({
        steps: [
            {
                id: 1,
                name: "商品策划",
                children: [
                    {
                        id: 2,
                        pid: 1,
                        name: "商品品类/标题",
                        requiredNumber: 2
                    },
                    {
                        id: 3,
                        pid: 1,
                        name: "商品规格",
                        requiredNumber: 4
                    }
                ]
            },
            {
                id: 4,
                name: "商品详情",
                children: [
                    {
                        id: 5,
                        pid: 4,
                        name: "上传图文",
                        requiredNumber: 1
                    }
                ]
            },
            {
                id: 6,
                name: "商品营销",
                children: [
                    {
                        id: 7,
                        pid: 6,
                        name: "列表封面",
                        requiredNumber: 1
                    },
                    {
                        id: 8,
                        pid: 6,
                        name: "主页头图",
                        requiredNumber: -1
                    },
                    {
                        id: 9,
                        pid: 6,
                        name: "商品标签",
                        requiredNumber: 1
                    }
                ]
            },
            {
                id: 10,
                name: "提交审核",
                children: [
                    {
                        id: 11,
                        pid: 10,
                        name: "预览提交",
                        requiredNumber: -1
                    }
                ]
            }
        ], // 创建商品步骤
        currentStepId: 2,
        hiddenSteps: [],
        formData: {
            standard: { // 标准版
                deliver: [],
                introduce: "",
                goodsAmount: null,
                versionType: 1,
                deliverDays: null
            },
            experience: { // 体验版
                deliver: [],
                introduce: "",
                goodsAmount: null,
                versionType: 2,
                deliverDays: null
            },
            introduce: { // 商品图文
                introduce: ''
            },
            coverPicList: [], // 商品封面图
            labelList: [], // 商品标签
        },
        categoryList: [], // 商品品类列表
        openTrialVersion: false, // 是否开启体验版

    }),
    getters: {},
    actions: {
        setOpenTrialVersion(value) {
            console.log('更新体验版开启状态', value)
            this.openTrialVersion = value;
        },
        setCurrentStepId(id) {
            this.currentStepId = id
        },
        setRequiredNumber(id, num) {
            this.steps.forEach(item => {
                item.children.forEach(child => {
                    if (child.id === id) {
                        child.requiredNumber = num
                    }
                })
            })
        },
        setHideSteps(id) {
            // 存在删除，不存在添加
            if (this.hiddenSteps.includes(id)) {
                this.hiddenSteps = this.hiddenSteps.filter(item => item !== id)
            } else {
                this.hiddenSteps.push(id)
            }
        },
        async getTypeList() {
            try {
                const res = await getAllType()
                if (res.code === 200) {
                    this.categoryList = res.data
                }
            } catch (e) {
                console.log(e)
            }
        },
        setGoodsInfo(data) {
            this.formData = data
        },
        initFormData() {
            this.formData = null;
            this.formData = {
                standard: { // 标准版
                    deliver: [
                        { content: "" },
                        { content: "" },
                        { content: "" },
                    ],
                    introduce: "",
                    goodsAmount: null,
                    versionType: 1,
                    deliverDays: null
                },
                experience: { // 体验版
                    deliver: [
                        { content: "" },
                        { content: "" },
                        { content: "" },
                    ],
                    introduce: "",
                    goodsAmount: null,
                    versionType: 2,
                    deliverDays: null
                },
                introduce: { // 商品图文
                    introduce: ''
                },
                coverPicList: [], // 商品封面图
                labelList: [], // 商品标签
            }
            this.currentStepId = 2
            this.openTrialVersion = false
            this.steps = [
                {
                    id: 1,
                    name: "商品策划",
                    children: [
                        {
                            id: 2,
                            pid: 1,
                            name: "商品品类/标题",
                            requiredNumber: 2
                        },
                        {
                            id: 3,
                            pid: 1,
                            name: "商品规格",
                            requiredNumber: 4
                        }
                    ]
                },
                {
                    id: 4,
                    name: "商品详情",
                    children: [
                        {
                            id: 5,
                            pid: 4,
                            name: "上传图文",
                            requiredNumber: 1
                        }
                    ]
                },
                {
                    id: 6,
                    name: "商品营销",
                    children: [
                        {
                            id: 7,
                            pid: 6,
                            name: "列表封面",
                            requiredNumber: 1
                        },
                        {
                            id: 8,
                            pid: 6,
                            name: "主页头图",
                            requiredNumber: -1
                        },
                        {
                            id: 9,
                            pid: 6,
                            name: "商品标签",
                            requiredNumber: 1
                        }
                    ]
                },
                {
                    id: 10,
                    name: "提交审核",
                    children: [
                        {
                            id: 11,
                            pid: 10,
                            name: "预览提交",
                            requiredNumber: -1
                        }
                    ]
                }
            ]
        },
    }
})